package org.bbl.utils;

import org.bbl.common.ProjectResources;

import java.util.Map;

/**
 * User: biniamg
 * Date: Jul 16, 2010
 */
public class GeezUtil {


    static Map<String, String> sera = IoUtils.processCSV(ProjectResources.getResourceFile("keyboard", "sera.csv"));

    public static boolean isGeez(String s) {
        return (s.length() > 0 && (s.charAt(0) > 4000 || s.equals(" ") || s.equals("\t") || s.equals("-") || s.equals(":")));
    }

    public static String removeLatin(String vs) {
        if (vs == null || vs.length() < 1) {
            return vs;
        }

        StringBuilder builder = new StringBuilder();
        for (String s : vs.split("")) {
            if (s.length() > 0 && (s.charAt(0) > 4000 || s.equals(" "))) {
                builder.append(s);
            }

        }
        return builder.toString().trim();
    }

    public static String toSera(String utf8) {
        if (utf8 == null) {
            return null;
        }
        StringBuilder builder = new StringBuilder();
        for (String str : utf8.split("\\s+")) {

            for (char s : str.toCharArray()) {
                String seraString = sera.get(String.valueOf((int) s));
                if (seraString != null) {
                    builder.append(seraString);
                }
            }
            builder.append(" ");
        }
        return builder.toString().trim();
    }

    public static String removeVowel(String s) {
        return s.replaceAll("[a|e|i|o|u|A|E|O|U|\\'|\\`|_]", "");
    }

    public static String toSeraNoVowelYesI(String utf8) {
        if (utf8 == null) {
            return null;
        }
        StringBuilder builder = new StringBuilder();
        for (String str : utf8.split("\\s+")) {

            for (char s : str.toCharArray()) {
                if ((s >= 4768 && s <= 4774) || (s >= 4816 && s <= 4822)) {
                    builder.append("I");
                } else {
                    String seraString = sera.get(String.valueOf((int) s));
                    if (seraString != null) {
                        builder.append(seraString);
                    }
                }
            }
            builder.append(" ");
        }
        return removeVowel(builder.toString().trim());
    }

}

